package mo.offer_special;

import java.util.HashMap;
import java.util.Map;

public class L011 {

    public int findMaxLength(int[] nums) {
        int maxLength = 0;
        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;
        map.put(count, -1);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                count++;
            } else {
                count--;
            }

            if (map.containsKey(count)) {
                int pre = map.get(count);
                maxLength = Math.max(maxLength, i-pre);
            } else {
                map.put(count, i);
            }
        }
        return maxLength;
    }

}
